package com.fsc.fragment.login;

import java.util.ArrayList;
import java.util.List;

import com.fsc.fragment.DBHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class PasswordDao {
	
	private DBHelper dbHelper;
	private Password objPassword;
	
	public PasswordDao(Context context) {
		dbHelper = new DBHelper(context);
	}
	//新增
	public void insert(Password password){
		//创建连接
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		//调用方法
		ContentValues values = new ContentValues();
		//values.put("id", password.getId());
		Log.e("fsc", password.getName());
		values.put("name", password.getName());
		database.insert("Password", null, values);
		
		//关闭连接
		database.close();
	}
	//查询
	public List<Password> query(){
		//创建连接
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		List<Password> list = new ArrayList<Password>();
		//调用方法
		Cursor cursor = database.query("Password", null, null, null, null, null, null);
		while(cursor.moveToNext()){
			int id = cursor.getInt(0);
			String name = cursor.getString(1);
			list.add(new Password(id,name));
		}
		//关闭连接
		cursor.close();
		database.close();
		return list;
	}
	
	public Password getName(String name){
		objPassword = new Password();
		
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		Cursor cursor = database.query("Password", null, "name=?", new String[]{name}, null, null, null);
		while(cursor.moveToNext()){
			objPassword.setName(cursor.getString(1));
		}
		cursor.close();
		database.close();
		return objPassword;
	}
	
	//根据id查询
	public int getAll(String name){
		//创建连接
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		
		//调用方法
		Cursor cursor = database.query("Password", null, "name=?", new String[]{name}, null, null, null);
		int count = cursor.getCount();
		//关闭连接
		cursor.close();
		database.close();
		return count;
	}
	//更新
	public void update(Password password){
		//创建连接
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		//调用方法
		ContentValues values = new ContentValues();
		values.put("name", password.getName());
		database.update("Password", values , "_id="+password.getId(),null);
		//关闭连接
		database.close();
	}
	
	//删除
	public void delete(int id){
		//创建连接
		//调用方法
		//关闭连接
	}
}
